<template>
	<view class="shareBonusRecord-container">
		<view class="top-box flexWrapNo">
			<view class="search-input flexWrapNo">
				<input type="tel" placeholder="请输入手机号码" maxlength="11" placeholder-class="placeholderClass" v-model="mobile"/>
			</view>
		<!-- 	<picker mode="date" fields="month" :value="date" @change="bindDateChange">
				<view class="date-input" v-if="date">{{date}}</view>
				<view class=" date-input placeClass" v-else>选择年月</view>
			</picker> -->
			
			<picker mode="date" fields="month" :data-index='0' :value="startTime" @change="bindDateChange" :end="startLimit">
				<view class="select-input" v-if="startTime">{{startTime}}</view>
				<view class="placeClass" v-else>开始日期</view>
			</picker>
			~
			<picker mode="date" fields="month" :data-index='1' :value="date" @change="bindDateChange" :start="startTime" :end="currentDate">
				<view class="select-input" v-if="endTime">{{endTime}}</view>
				<view class="placeClass" v-else>结束日期</view>
			</picker>
			<view class="search-btn flexWrapNo" @tap="freshList()">查询</view>
		</view>
		
		<!-- 列表数据 -->
		<view class="table-list" v-if="list.length>0">
			<view class="table-item table-head">
				<view class="date">日期</view>
				<view class="mobile">手机号码</view>
				<view class="bonus">分红</view>
				<view class="status">状态</view>
				<view class="operate">操作</view>
			</view>
			<view class="table-item" v-for="(item,index) in list" :key="index">
				<view class="date">{{item.date}}</view>
				<view class="mobile">{{item.mobile}}</view>
				<view class="bonus">{{item.divide}}</view>
				<view class="status yellow" v-if="item.divide==0">无需发放</view>
				<view class="status" :class="{yellow:item.status==1}" v-else>{{item.status==1?'待发放':'已发放'}}</view>
				<view class="operate">
					<block v-if="item.divide==0">
						<view>--</view>
					</block>
					<block v-else>
						<view v-if="item.status==1" @tap="handleGrant(item)">发放</view>
						<view v-else @tap="lookAttachment(item)">查看附件</view>
					</block>
				</view>
			</view>
		</view>
		
		<uni-load-more v-if="showLoadStatus" :status="status" :size="14" :content-text="contentText" />
		<view class="quesheng" v-if="showEmpty">
			<image src="https://static.mo-yu.vip/wechat/images/orderEmpty.png"></image>
		</view>
		
		<!-- 底部弹窗 -->
		<uni-popup ref="popup" type="bottom" @maskClick="closePopup">
			<view class="popup-content">
				<view class="basic-info">
					<view class="receiver">收款人</view>
					<view class="info">
						<view>{{bankInfo||'-'}}</view>
						<!-- <view>61000000000000</view> -->
					</view>
				</view>
				
				<view class="upload-box">
					<view class="label">附件上传：</view>
					<view class="attachment-box flexWrapNo">
						<view @tap="previewImage(item,imageList)" class="attachment-img"
							v-for="(item,index) in imageList" :key="index">
							<image :src="item" mode="aspectFill"></image>
						<!-- 	<image src="@/static/user/close.png" class="close-btn"
								@tap.stop="deleteImage(index)"></image> -->
								<view class="close-btn" @tap.stop="deleteImage(index)">
									<uni-icons type="closeempty" size="14" color="#fff"></uni-icons>
								</view>
						</view>
						<view class="add-btn" @tap="chooseImage()" v-if="imageList.length<3"></view>
					</view>
					<view class="attachment-tips">请上传发放凭证，最多上传3张图片，仅支持jpg、jpeg、png格式图片</view>
				</view>
				
				<view class="submit-btn" @tap="submit">确认发放</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import utils from '@/common/lib/util.js'
	import base from '@/common/base.js';
	export default{
		data(){
			const currentDate = this.getDate({
				format: true
			})
			return{
				mobile:'',
				type:null,
				bankInfo:'',
				startTime:null,
				endTime:null,
				currentDate:currentDate,
				startLimit:currentDate,
				list:[],
				recordId:'', // 分红记录id
				imageLength:3,
				imageList:[],
				uploadList:[],
				
				page: 1,
				size: 30,
				showEmpty: true,
				showLoadStatus: false,
				more: true,
				contentText: {
					contentdown: '上拉加载更多~',
					contentrefresh: '加载中...',
					contentnomore: '已经到底了~~'
				},
				status: 'loading',
			}
		},
		watch:{
			endTime(val){
				if(val){
					this.startLimit=val
				}else{
					this.startLimit=this.currentDate
				}
			},
		},
		onLoad(options){
			this.mobile=options.mobile
			this.type=options.type
			this.getList()
		},
		
		onPullDownRefresh() {
			this.freshList()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 500)
		},
		
		onReachBottom() {
			if (this.more) {
				this.getList()
			}
		},
		methods:{
			// 获取日期 --当月
			getDate(type) {
				const date = new Date();
				date.setDate(date.getDate());
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				// return `${year}-${month}-${day}`
				return `${year}-${month}`
			},
			
			freshList:utils.debounce(function(){
				this.page = 1
				this.more = true
				this.status = 'loading'
				this.list = []
				this.getList();
			},600),
			
			//  获取分红记录列表
			getList(){
				this.$request.post(this.$api.bonusList, {
					storeId:uni.getStorageSync('store_id'),
					mobile:this.mobile,
					start:this.startTime,
					end:this.endTime,
					status:this.type,
					pageNum:this.page++,  //当前页数
					pageSize:this.size,  //当前条数
				}, {
					isJava: true
				}).then(res => {
					if(res.code==200){
						let {
							data = {}
						} = res;
						let newArray = data.list
						
						if (newArray.length == 0 && this.page == 2) {
							this.showEmpty = true
							this.showLoadStatus = false
							this.more = false // 不能再次请求了
							return false;
						} else {
							this.showLoadStatus = true
							this.more = true
							this.showEmpty = false
							this.status = 'loading'
						}
						if (newArray.length < this.size) { // 说明没有更多数据
							this.status = 'noMore'
							this.more = false
							this.showEmpty = false
						}
						this.list = this.list.concat(newArray);
					}
				})
			},
			
			// 选择时间
			bindDateChange(e) {
				let index = e.currentTarget.dataset.index
				if (index == 0) {
					this.startTime = e.target.value
				} else {
					this.endTime = e.target.value
				}
			},
			
			handleGrant(item){
				this.$refs.popup.open()
				this.recordId=item.id
				this.bankInfo=item.bankInfo
			},
			
			//删除图片
			deleteImage(index) {
				this.imageList.splice(index, 1);
				this.uploadList.splice(index, 1);
			},
			
			chooseImage: async function() {
				var that = this
				uni.chooseImage({
					count: this.imageLength - this.imageList.length,
					extension: ['.jpg', '.png', '.jpeg'],
					success: (res) => {
						console.log(res)
						for (let i in res.tempFilePaths) {
							var imgFiles = res.tempFilePaths[i]
							let suffix = res.tempFilePaths[i].split('.')[1]
							// console.log(suffix)
							if (suffix != 'jpg' && suffix != 'png' && suffix != 'jpeg') {
								this.$tools.showToast('图片仅支持jpg、jpeg、png');
								return;
							}
							that.cutImageCallback(imgFiles);
						}
						that.imageList = that.imageList.concat(res.tempFilePaths)
					}
				})
			},
			
			cutImageCallback(content) {
				let that = this;
				let httpsHead = 'https://moyu24h-new.oss-cn-chengdu.aliyuncs.com/'
				that.$tools.loadIng('上传中,请稍后..')
				base.aliyun_upload(content, function(r, err) {
					if (r) {
						let uploadimg = httpsHead + r
						console.log(r)
						that.uploadList = that.uploadList.concat(uploadimg);
						uni.hideLoading();
					}
				})
			},
			
			// 查看附件
			lookAttachment(item){
				let imageList
				if(item.attachment){
					imageList=item.attachment.split(',')
					this.previewImage(0,imageList)
				}else{
					this.$tools.showToast('无附件可查看')
				}
			},
			
			closePopup(){
				this.$refs.popup.close()
				this.imageList=[]
				this.uploadList=[]
			},
			
			submit:utils.debounce(function(){
				if(this.uploadList.length==0){
					this.$tools.showToast('请上传附件')
					return
				}
				this.$request.post(this.$api.grantBonus, {
					id:this.recordId,
					attachment:this.uploadList.join(',')
				}, {
					isJava: true
				}).then(res => {
					if(res.code==200){
						let {
							data = {}
						} = res;
						this.closePopup()
						this.$tools.showToast('发放成功')
						this.getList()
					}
				})
			},1000),
		},
	}
</script>

<style lang="scss" scoped>
	.shareBonusRecord-container{
		padding: 0 28rpx;
		padding-bottom: 80rpx;
	}
	.top-box{
		padding: 30rpx 0;
		gap: 20rpx;
		background: #040336;
		position: sticky;
		top: 0;
		color: #fff;
		/deep/.search-input{
			width: 200rpx;
			height:60rpx;
			border-radius:8rpx;
			border: 2rpx solid rgba(255,255,255,0.5);
			padding: 0 16rpx;
			input{
				font-size: 28rpx;
				color: #FFF;
				width: 100%;
			}
			.placeholderClass{
				font-size: 28rpx;
				color: rgba(255,255,255,0.5);
			}
		}
		picker{
			flex: 1;
			height:60rpx;
			line-height: 60rpx;
			color: #fff;
			box-sizing: border-box;
			border-radius:8rpx;
			font-size: 28rpx;
			.placeClass{
				font-size: 28rpx;
				color: rgba(255,255,255,0.5);
			}
		}
		.search-btn{
			padding: 0 30rpx;
			height: 60rpx;
			border-radius: 8rpx;
			color: #fff;
			font-size: 28rpx;
			background: rgb(31, 124, 247);
		}
	}
	
	.table-list{
		margin-top: 10rpx;
		background: #1D1C5C;
		border-radius: 8rpx;
		.table-item{
			display: flex;
			color: #fff;
			font-size: 24rpx;
			text-align: center;
			border-bottom:1rpx solid  rgba(255,255,255,0.3);
			&:last-child{
				border-bottom: unset;
			}
			>view{
				padding: 16rpx 10rpx;
				box-sizing: border-box;
				display: flex;
				justify-content: center;
				align-items: center;
			}
			.date{
				width: 170rpx;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.mobile{
				width: 170rpx;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.bonus{
				flex: 1;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.status{
				width: 120rpx;
				border-right:2rpx solid  rgba(255,255,255,0.3);
			}
			.yellow{
				color: #FFE577;
			}
			.operate{
				width: 130rpx;
				>view{
					color: #26BCFD;
				}
			}
		}
		.table-head{
			>view{
				font-weight: bold;
				font-size: 26rpx !important;
				padding: 14rpx 10rpx;
			}
		}
	}
	
	.quesheng{
		margin-top: 30rpx;
		display: flex;
		justify-content: center;
		image{
			width: 476upx;
			height: 280upx;
		}
	}
	
	.popup-content{
		background: #fff;
		padding: 28rpx 28rpx 50rpx 28rpx;
		border-top-left-radius: 16rpx;
		border-top-right-radius: 16rpx;
		.basic-info{
			padding-bottom: 10rpx;
			border-bottom: 1rpx solid rgba(0, 0, 0, 0.2);
			.receiver{
				color: #666;
				font-size: 28rpx;
				line-height: 46rpx;
			}
			.info{
				color: #000;
				font-size: 28rpx;
				>view{
					line-height: 46rpx;
				}
			}
		}
		.upload-box{
			margin-top: 20rpx;
			.label{
				font-size: 28rpx;
			}
			.attachment-box {
				margin-top: 20rpx;
				flex-wrap: wrap;
				gap: 20rpx;
				.add-btn {
					width: 156rpx;
					height: 156rpx;
					border-radius: 8rpx 8rpx 8rpx 8rpx;
					border: 1upx solid  rgba(0, 0, 0, 0.2);
					position: relative;
					flex-shrink: 0;
			
					&::after {
						content: "";
						display: inline-block;
						width: 40upx;
						height: 4upx;
						background: rgba(0, 0, 0, 0.2);
						position: absolute;
						left: 0;
						right: 0;
						top: 0;
						bottom: 0;
						margin: auto;
					}
			
					&::before {
						content: "";
						display: inline-block;
						width: 4upx;
						height: 40upx;
						background: rgba(0, 0, 0, 0.2);
						position: absolute;
						left: 0;
						right: 0;
						top: 0;
						bottom: 0;
						margin: auto;
					}
				}
			
				.attachment-img {
					width: 156rpx;
					height: 156rpx;
					border-radius: 8rpx 8rpx 8rpx 8rpx;
					position: relative;
					flex-shrink: 0;		
					.close-btn {
						position: absolute;
						top: -10rpx;
						right: -10rpx;
						width: 40rpx;
						height: 40rpx;
						line-height: 40rpx;
						background: #999;
						border-radius: 50%;
						display: flex;
						justify-content: center;
					}
				}
			}
			
			.attachment-tips {
				font-size: 24upx;
				margin-top: 20upx;
			}
		}
		
		.submit-btn{
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			width: 600rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background: rgb(31, 124, 247);
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin: 50rpx auto;
		}
	}
</style>